package com.huike.contract.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.clues.domain.SysUser;
import com.huike.clues.mapper.SysUserMapper;
import com.huike.common.core.domain.entity.SysUserDTO;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.MessageUtils;
import com.huike.contract.domain.dto.TransferPageQueryDTO;
import com.huike.contract.domain.vo.TransferVo;
import com.huike.contract.mapper.TransferMapper;
import com.huike.contract.result.TransferAssignmentData;
import com.huike.contract.service.TransferService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhu
 * @Description: TransferServiceImpl
 * @DateTime: 2023/10/17 2:27
 **/
@AllArgsConstructor
@Service
@Slf4j
public class TransferServiceImpl implements TransferService {

    private TransferMapper transferMapper;

    private SysUserMapper sysUserMapper;

    /**
     * 获取转派列表
     * @param transferPageQueryDTO
     * @return
     */
    @Override
    public TableDataInfo<List<TransferVo>> pageTransfer(TransferPageQueryDTO transferPageQueryDTO) {
        PageHelper.startPage(transferPageQueryDTO.getPageNum(), transferPageQueryDTO.getPageSize());


        if (transferPageQueryDTO.getParams().get("beginTime") != null && !(transferPageQueryDTO.getParams().get("beginTime").equals(""))
                && transferPageQueryDTO.getParams().get("endTime") != null && !(transferPageQueryDTO.getParams().get("endTime").equals("")))
        {
            String beginCreateTime = (String) transferPageQueryDTO.getParams().get("beginTime");
            String endCreateTime = (String) transferPageQueryDTO.getParams().get("endTime");
            transferPageQueryDTO.setBeginTime(beginCreateTime);
            transferPageQueryDTO.setEndTime(endCreateTime);
        }
        Page<TransferVo> page = (Page<TransferVo>) transferMapper.pageTransfer(transferPageQueryDTO);

        TableDataInfo<List<TransferVo>> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getResult());

        return tableDataInfo;
    }

    /**
     * 转派处理
     *
     * @param transferUserId
     * @param userId
     * @param type
     * @return
     */
    @Override
    public TransferAssignmentData transfer(Integer transferUserId, Integer userId, String type) {
        Integer count1 = transferMapper.findCountByTypeAndId(userId, type);
        Integer count2 = transferMapper.findCountByTypeAndId(transferUserId, type);

        Integer max = transferMapper.findMaxNumber(type);

        SysUserDTO transferUser = sysUserMapper.selectUserById(Long.valueOf(transferUserId));
        String transferUserName = transferUser.getUserName();
        Long transferUserDeptId = transferUser.getDeptId();

        TransferAssignmentData transferAssignmentData = new TransferAssignmentData();
        if(count1 + count2 > max) {
            transferAssignmentData.setFlag(false);
            if(type.equals("0")) {
                transferAssignmentData.setMsg("线索转换失败！已经达到被转派人最大保有量");
            } else transferAssignmentData.setMsg("商机转换失败！已经达到被转派人最大保有量");
            return transferAssignmentData;
        }

        transferMapper.updateAssignId(transferUserId, userId, type, transferUserName, transferUserDeptId);
        transferAssignmentData.setFlag(true);
        if(type.equals("0")) {
            transferAssignmentData.setMsg("线索转换成功");
        } else transferAssignmentData.setMsg("商机转换成功");

        return transferAssignmentData;
    }
}